//
//  string+extension.swift
//  WSWB
//
//  Created by 刘 伟生 on 16/1/12.
//  Copyright © 2016年 seasa. All rights reserved.
//

import UIKit

extension String
{
    func docDir() -> String
    {
        // 1.拿到文件夹路径
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 2.拼接字符串
        // 注意: pathComponents方法, 会按照字符串的/来切割字符串, 如果给定的字符串中没有/那么就直接返回给定的字符串
        let temp = (self as NSString).pathComponents.last
        
        // 如果??前面有值, 那么??后面的代码不执行, 如果??前面没有值, 那么就执行??后面的代码
        let result = (path as NSString).stringByAppendingPathComponent(temp ?? "")
        
        // 3.返回结果
        return result
    }
    
    func cachesDir() -> String
    {
        // 1.拿到文件夹路径
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        let temp = (self as NSString).pathComponents.last
        // 2.拼接字符串
        let result = (path as NSString).stringByAppendingPathComponent(temp ?? "")
        
        // 3.返回结果
        return result
    }
    
    func tmpDir() -> String
    {
        // 1.拿到文件夹路径
        let path = NSTemporaryDirectory()
        
        let temp = (self as NSString).pathComponents.last
        // 2.拼接字符串
        let result = (path as NSString).stringByAppendingPathComponent(temp ?? "")
        
        // 3.返回结果
        return result
    }
}
